package pdf;
import java.awt.Desktop;
import java.io.File;
import java.io.FileOutputStream;
import java.io.OutputStream;
import java.text.SimpleDateFormat;
import java.util.Calendar;
import java.util.Date;











import rentReturnEntity.ReturnCostEntity;
import rentReturnEntity.ReturnEntity;

import com.itextpdf.text.Document;
import com.itextpdf.text.Font;
import com.itextpdf.text.Image;
import com.itextpdf.text.Paragraph;
import com.itextpdf.text.pdf.PdfWriter;

public class PDFGenerator {

	  public static void  generateRentalAgreement(long aggrementNumber, String name,String vehicleType,int vehicleId,String returnDate) {

	        try {
	        	String fileName = "RentalAgreement_"+ aggrementNumber +".pdf";
	            OutputStream file = new FileOutputStream(new File(fileName));
	            Calendar cal = Calendar.getInstance();
	            SimpleDateFormat format = new SimpleDateFormat("MM-dd-yyyy");
	            String pickUpdate = format.format(cal.getTime());
	            Font myContentStyle=new Font();
	            Document document = new Document();

	            PdfWriter.getInstance(document, file);
	            myContentStyle.setStyle("bold");
	            

	            document.open();
	            /*Image image = Image.getInstance("\\SuperRentManagement\\image\\Black_and_white_512.png");
	            document.add((image));*/
	            
	            document.add(new Paragraph("SuperRent Rental Agreement Form",myContentStyle));

	            document.add(new Paragraph("This Agreement (the 'Agreement') dated this " + new Date().toString()));
	            document.add(new Paragraph("Between"));
	            document.add(new Paragraph("'SuperRent' of Fraser Street, Vancouver, BC, V5X 3M5",myContentStyle));
	            document.add(new Paragraph("(the 'Renter')"));
	            document.add(new Paragraph("-and-"));
	            document.add(new Paragraph(name,myContentStyle));
	            document.add(new Paragraph("(the 'Rentee')"));
	            document.add(new Paragraph("(Renter and Rentee are collectively the Parties)"));
	            document.add(new Paragraph(""));
	            document.add(new Paragraph("IN CONSIDERATION of the mutual covenants and  promises in this Agreement, the receipt and sufficiency of which consideration the Parties acknowledge, the Renter rents the Property to the Rentee on the following terms:"));
	            document.add(new Paragraph("DEFINITIONS"));
	            document.add(new Paragraph("1. The following definitions are used but  not otherwise defined in this Agreement:"));
	            document.add(new Paragraph("a. 'Casualty Value' means the market value of the property at the end of the Term or when in relation to 	a Total Loss, the market value the Property would have had at the end of the Term but for the Total Loss. The Casualty Value may be Less than but will not be more than the original purchase price of the property."));
	            document.add(new Paragraph("b. 'Property' means Vehicle (Car/Truck)"));
	            document.add(new Paragraph("c. 'Lease' means this Agreement"));
	            document.add(new Paragraph("d. 'Total Loss' means any loss of or damage that is not repairable or that would cost more to repair than the market value of the Property."));
	            document.add(new Paragraph("Lease"));
	            document.add(new Paragraph("2. The Renter agrees to lease the Property to the Rentee, and the Rentee agrees to Renter the Property from the Renter in accordance with the terms provided in this Agreement."));
	            document.add(new Paragraph("Term"));
	            document.add(new Paragraph("3. The Rentee will rent the vehicle " + vehicleType + "with vehicle Id "+ vehicleId + "which is a property of the Renter from " + pickUpdate + "Untill the day of " + returnDate  +" Failure to return on specified date, will result in additional fees")); 
	            document.add(new Paragraph("In order to Rent a Property from SuperRent, the Rentee should read this agreement form, write and tick the corresponding wherever applicable and sign this rental agreement form."));
	            document.add(new Paragraph("   Signature of the Renter	",myContentStyle));
	            

	            document.close();

	            file.close();
	            if (Desktop.isDesktopSupported()) {
		             try {
		                 File myFile = new File(fileName);
		                 Desktop.getDesktop().open(myFile);
		             } catch (Exception e) {
		                 System.out.println(e.getMessage());
		             }
		         }


	        } catch (Exception e) {


	            e.printStackTrace();

	        }

	    }
	  
	  public static void  generateReturnReceipt(ReturnEntity cost, String paymentId) {

	        try {
	        	String fileName = "RentalReceipt_" + paymentId +".pdf";
	            OutputStream file = new FileOutputStream(new File(fileName));
	            Calendar cal = Calendar.getInstance();
	            SimpleDateFormat format = new SimpleDateFormat("MM-dd-yyyy");
	            String date = format.format(cal.getTime());
	            Font myContentStyle=new Font();
	            Document document = new Document();

	            PdfWriter.getInstance(document, file);
	            myContentStyle.setStyle("bold");
	            

	            document.open();
	            /*Image image = Image.getInstance("\\SuperRentManagement\\image\\Black_and_white_512.png");
	            document.add((image));*/
	            
	            document.add(new Paragraph("SuperRent Rental Receipt For Rental",myContentStyle));

	            document.add(new Paragraph("Dated " + date));
	            document.add(new Paragraph("Payment Id " + paymentId));
	            document.add(new Paragraph("ReturnId" + cost.getReturnId()));
	            document.add(new Paragraph("fees : " + cost.getFees() + " CAD"));
	            document.add(new Paragraph("rentalCost : "+cost.getRentalCost()+ " CAD"));
	            document.add(new Paragraph("insCost : "+cost.getInsCost()+ " CAD"));
	            document.add(new Paragraph("dedInsCost : "+cost.getDedInsCost()+ " CAD"));
	            document.add(new Paragraph("tax : "+cost.getTax()+ " CAD"));
	            document.add(new Paragraph("fine : "+cost.getFine()+ " CAD"));
	            document.add(new Paragraph("damageCost : "+cost.getDamageCost()+ " CAD"));
	            document.add(new Paragraph("miscCharges : "+cost.getMiscCharges()+ " CAD"));
	            document.add(new Paragraph("totalCost : "+cost.getTotalCost() + " CAD"));
	            document.add(new Paragraph("finalCost : "+cost.getFinalCost()+ " CAD"));

	            document.close();

	            file.close();
	            if (Desktop.isDesktopSupported()) {
		             try {
		                 File myFile = new File(fileName);
		                 Desktop.getDesktop().open(myFile);
		             } catch (Exception e) {
		                 System.out.println(e.getMessage());
		             }
		         }


	        } catch (Exception e) {


	            e.printStackTrace();

	        }

	    }
}
